//
//  FirCirLocalResourceDataSource.m
//  tcpoctest
//
//  Created by gump on 4/16/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "FirCirLocalResourceDataSource.h"
#import "LocalPathDataSource.h"
#import "FriendStatus.h"
#import "FirendCirLocalPathdb.h"

@implementation FirCirLocalResourceDataSource

-(id)init
{
    self = [super init];
    if (self != nil) {
        FCLocalRsDic = [[NSMutableDictionary alloc] init];
        pthread_mutex_init(&localrsmutex, 0);
        downloadDic = [[NSMutableDictionary alloc] init];
    }
    
    return self;
}

-(void)RemoveAllLocalRs
{
    pthread_mutex_lock(&localrsmutex);
    
    [FCLocalRsDic removeAllObjects];
    
    pthread_mutex_unlock(&localrsmutex);
}

-(void)AddLocalRs:(FirCirLocalInfo *)fclinfo
{
    pthread_mutex_lock(&localrsmutex);
    
    [FCLocalRsDic setObject:fclinfo forKey:fclinfo.resourceID];
    
    pthread_mutex_unlock(&localrsmutex);
}

-(NSString *)getRcLocalPathByID:(NSString *)strid
{
    pthread_mutex_lock(&localrsmutex);
    
    FirCirLocalInfo *fclinfo = [FCLocalRsDic objectForKey:strid];
    
    NSString *strpath = @"";
    
    do {
        if (fclinfo == nil) {
            break;
        }
        else
        {
            if ([fclinfo.localpath isEqualToString:@""]) {
                if (![fclinfo.url isEqualToString:@""]) {
                    
                    [downloadDic setObject:fclinfo.url forKey:fclinfo.resourceID];
                    
                    [[DataControl shareControl].LCDS addLogicCommand:LC_FCR_DOWNLOAD];
                }
            }
            else
            {
                strpath = fclinfo.localpath;
            }

        }
    } while (false);
    
    pthread_mutex_unlock(&localrsmutex);
    
    return strpath;
}

-(void)DownLoadResource
{
    pthread_mutex_lock(&localrsmutex);
    
    NSArray *downarr = [downloadDic allKeys];
    
    for (NSString *strid in downarr) {
        
        NSString *path = [downloadDic objectForKey:strid];
        
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:path]];
        
        NSString *localpath = [self creatRcLocalPath];
        if ([data writeToFile:localpath atomically:YES] == YES) {
            FirCirLocalInfo *flinfo = [FCLocalRsDic objectForKey:strid];
            if (flinfo != nil) {
                flinfo.localpath = localpath;
                [[DataBaseCtrol shareControl].fircirlocaldatabase insertLocalPath:flinfo];
            }
            
            
        }
    }
    
    [downloadDic removeAllObjects];
    
    pthread_mutex_unlock(&localrsmutex);
}

-(NSString *)creatRcLocalPath
{
    NSString *strname = stringWithUUID();
    strname = [NSString stringWithFormat:@"%@%@",strname,[DataControl shareControl].personDS.uinfo.mail];
    NSString *strpath = [[[DataControl shareControl].localpathds LPFircirPath]
                         stringByAppendingPathComponent:strname];
    return strpath;
}

-(void)DelLocalByID:(NSString *)strid
{
    pthread_mutex_lock(&localrsmutex);
    
    FirCirLocalInfo *flinfo = [FCLocalRsDic objectForKey:strid];
    if (flinfo != nil) {
        [[FileManager shareControl] deleteFile:flinfo.localpath];
        [FCLocalRsDic removeObjectForKey:strid];
    }
    
    pthread_mutex_unlock(&localrsmutex);
}

@end
